package com.example.remotecontrol

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Devices
import androidx.compose.material.icons.filled.RemoteControl
import androidx.compose.material.icons.filled.Scene
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.example.remotecontrol.ui.devices.DevicesScreen
import com.example.remotecontrol.ui.navigation.RemoteNavGraph
import com.example.remotecontrol.ui.scenes.ScenesScreen
import com.example.remotecontrol.ui.settings.SettingsScreen
import com.example.remotecontrol.ui.theme.RemoteControlTheme
import dagger.hilt.android.AndroidEntryPoint

/**
 * 主活动
 * 负责应用的导航和主界面
 */
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            RemoteControlTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    RemoteControlApp()
                }
            }
        }
    }
}

/**
 * 应用主界面
 * 包含底部导航和各个主要页面
 */
@Composable
fun RemoteControlApp() {
    val navController = rememberNavController()
    
    Scaffold(
        bottomBar = {
            BottomNavBar(navController)
        }
    ) { innerPadding ->
        NavHost(
            navController = navController,
            startDestination = Screen.Remote.route,
            modifier = Modifier.padding(innerPadding)
        ) {
            // 遥控器导航图
            composable(Screen.Remote.route) {
                RemoteNavGraph()
            }
            
            // 设备页面
            composable(Screen.Devices.route) {
                DevicesScreen()
            }
            
            // 场景页面
            composable(Screen.Scenes.route) {
                ScenesScreen()
            }
            
            // 设置页面
            composable(Screen.Settings.route) {
                SettingsScreen()
            }
        }
    }
}

/**
 * 底部导航栏
 */
@Composable
fun BottomNavBar(navController: NavHostController) {
    val items = listOf(
        Screen.Remote,
        Screen.Devices,
        Screen.Scenes,
        Screen.Settings
    )
    
    NavigationBar {
        val navBackStackEntry by navController.currentBackStackEntryAsState()
        val currentDestination = navBackStackEntry?.destination
        
        items.forEach { screen ->
            NavigationBarItem(
                icon = { Icon(screen.icon, contentDescription = null) },
                label = { Text(screen.label) },
                selected = currentDestination?.hierarchy?.any { it.route == screen.route } == true,
                onClick = {
                    navController.navigate(screen.route) {
                        popUpTo(navController.graph.findStartDestination().id) {
                            saveState = true
                        }
                        launchSingleTop = true
                        restoreState = true
                    }
                }
            )
        }
    }
}

/**
 * 应用主要页面
 */
sealed class Screen(
    val route: String,
    val label: String,
    val icon: ImageVector
) {
    object Remote : Screen(
        route = "remote",
        label = "遥控器",
        icon = Icons.Default.RemoteControl
    )
    
    object Devices : Screen(
        route = "devices",
        label = "设备",
        icon = Icons.Default.Devices
    )
    
    object Scenes : Screen(
        route = "scenes",
        label = "场景",
        icon = Icons.Default.Scene
    )
    
    object Settings : Screen(
        route = "settings",
        label = "设置",
        icon = Icons.Default.Settings
    )
} 